#!/usr/bin/perl
#protocolman-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

sub list_protocols
{
#	print "<form name=protocolsform>\n";
	print "<table width=100% class=mode2><tr>\n";
	print "<td  valign=top><table class=mode1 border=0 bordercolor=#000000 cellspacing=0 cellpadding=1 width=100%>\n";
	print "<tr class=title1> <td><b>$text{'protocolname'}</b></td> ",
		"<td><b>$text{'status'}</b></td> ",
		"<td><b>$text{'description'}</b></td> </tr>\n";

	print "<tr class=mode1> <td><input type=radio name=protocol value='W''><img border=0 src=images/protocol.gif>$text{'cifs'}</td>\n";
	$cifs = &check_service_status("W");
	$cifs_status = ($cifs == 2)?"$text{'running_status'}":(($cifs == 1)?"$text{'critical_status'}":"$text{'stopped_status'}");
	printf "<td class=mode1>%s</td>\n", $cifs_status;
	print "<td>$text{'cifs_desc'}</td> </tr>\n";

	print "<tr class=mode2> <td><input type=radio name=protocol value='U'><img border=0 src=images/protocol.gif>$text{'nfs'}</td>\n";
	$nfs = &check_service_status("U");
	$nfs_status = ($nfs == 2)?"$text{'running_status'}":(($nfs == 1)?"$text{'critical_status'}":"$text{'stopped_status'}");
	printf "<td class=mode2>%s</td>\n", $nfs_status;
	print "<td>$text{'nfs_desc'}</td> </tr>\n";

	print "<tr class=mode1> <td><input type=radio name=protocol value='F'><img border=0 src=images/protocol.gif>$text{'ftp'}</td>\n";
	$ftp = &check_service_status("F");
	$ftp_status = ($ftp == 2)?"$text{'running_status'}":(($ftp == 1)?"$text{'critical_status'}":"$text{'stopped_status'}");
	printf "<td class=mode1>%s</td>\n", $ftp_status;
	print "<td>$text{'ftp_desc'}</td> </tr>\n";

	print "<tr class=mode2> <td><input type=radio name=protocol value='H'><img border=0 src=images/protocol.gif>$text{'http'}</td>\n";
	$http = &check_service_status("H");
	$http_status = ($http == 2)?"$text{'running_status'}":(($http == 1)?"$text{'critical_status'}":"$text{'stopped_status'}");
	printf "<td class=mode2>%s</td>\n", $http_status;
	print "<td>$text{'http_desc'}</td> </tr>\n";

	$afp = &check_afpserver_status();
	#print "<tr class=mode1> <td><input type=radio name=protocol value='A'><img border=0 src=images/protocol.gif>$text{'afp'}</td>\n";
#	$afp = &check_service_status("A");
	$afp_status = ($afp == 2)?"$text{'startpending_status'}":(($afp == 1)?"$text{'running_status'}":"$text{'stopped_status'}");
	#printf "<td class=mode1>%s</td>\n", $afp_status;
	#print "<td>$text{'afp_desc'}</td> </tr>\n";

	print "</table></td>\n";
	print "<td valign=top width=100><table class=mode1 border=0 cellspacing=4 cellpadding=1 width=100%>\n";
	print "<tr> <td align=center><input type=submit class=button1 name=startservice value='$text{'start_service'}', onclick='return set_oper_type(this)' style='border-width: 1'></td></tr>\n";
	print "<tr> <td align=center><input type=submit class=button1 name=stopservice value='$text{'stop_service'}', onclick='return set_oper_type(this)' style='border-width: 1'></td></tr>\n";
	print "<tr> <td align=center><input type=submit class=button1 name=restartservice value='$text{'restart_service'}', onclick='return set_oper_type(this)' style='border-width: 1'></td></tr>\n";
	print "<tr> <td align=center><input type=submit class=button1 name=serviceproperty value='$text{'service_property'}', onclick='return set_oper_type(this)' style='border-width: 1'></td></tr>\n";
	print "</table></td></tr>\n";
	print "</table>\n";
#	print "</form>\n";
}

sub check_service_status
{
	my ($totalstatus, $runningstatus) = (0, 0);
	if ($_[0] =~ /W/)
	{
		@st = `$config{'cifs_service'} status`;
	}
	elsif ($_[0] =~ /U/)
	{
		@st = `$config{'nfs_service'} status`;
	}
	elsif ($_[0] =~ /F/)
	{
		@st = `$config{'ftp_service'} status`;
	}
	elsif ($_[0] =~ /H/)
	{
		@st = `$config{'http_service'} status`;
	}
	elsif ($_[0] =~ /A/)
	{
		@st = `$config{'afp_service'} status`;
	}
	foreach $d (@st) {
		$totalstatus ++;
		if ($d =~ /running/)
		{
			$runningstatus ++;
		}
	}
	return 0 if (!$runningstatus);
	return 1 if ($runningstatus < $totalstatus);
	return 2;
}

#check_afpserver_status(void)
sub check_afpserver_status()
{
	my (@psatalkd, @psafp, $afppid, $atalkdpid, $natalk, $nps, @line);
	
	@psatalkd = `ps -A | grep atalkd`;

	@ps = `ps -A | egrep "afpd|atalkd"`;

	$natalk = @psatalkd;
	$nps = @ps;

	#atalkd progress not start, /etc/init.d/atalk not start, status is stopped
	return 0 if ($natalk == 0);
	
	#atalkd progress have completely started, afpd progress started, status is Running.
	if ($ps[$nps - 1] =~ /afpd/)
	{
		return 1;
	}

	#atalkd progress atart in background, afpd progress haven't started, status is start pending.
	return 2;
}

#remote_foreigncall for mdm module
#mdm_startcifs()
sub mdm_startcifs()
{
        my $out;

	$out = &check_service_status("W");
	if($out == 0)
	{
	    $out = `$config{'cifs_service'} start 2>&1`;
	}
	else
	{
	    return "CIFS ".$text{'err_msg_serv_already_start'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_stopcifs()
sub mdm_stopcifs()
{
        my $out;

	$out = &check_service_status("W");
	if($out != 0)
	{
	    $out = `$config{'cifs_service'} stop 2>&1`;
	}
	else
	{
	    return "CIFS ".$text{'err_msg_serv_already_stop'};
	}
	return "Success";

}

#remote_foreigncall for mdm module
#mdm_restartcifs()
sub mdm_restartcifs()
{
        my $out;
	$out = `$config{'cifs_service'} restart 2>&1`;

	return "Success";
}


#remote_foreigncall for mdm module
#mdm_startnfs()
sub mdm_startnfs()
{
        my $out;

	$out = &check_service_status("U");
	if($out == 0)
	{
	    $out = &ez_system($config{'nfs_service'}, "start");
	}
	else
	{
	    return "NFS ".$text{'err_msg_serv_already_start'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_stopnfs()
sub mdm_stopnfs()
{
        my $out;

	$out = &check_service_status("U");
	if($out != 0)
	{
	    $out = `$config{'nfs_service'} stop 2>&1`;
	}
	else
	{
	    return "NFS ".$text{'err_msg_serv_already_stop'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_restartnfs()
sub mdm_restartnfs()
{
        my $out;
	$out = &ez_system($config{'nfs_service'},  'restart');
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_startftp()
sub mdm_startftp()
{
        my $out;

	$out = &check_service_status("F");
	if($out == 0)
	{
	    $out = `$config{'ftp_service'} start 2>&1`;
	}
	else
	{
	    return "FTP ".$text{'err_msg_serv_already_start'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_stopftp()
sub mdm_stopftp()
{
        my $out;

	$out = &check_service_status("F");
	if($out != 0)
	{
	    $out = `$config{'ftp_service'} stop 2>&1`;
	}
	else
	{
	    return "FTP ".$text{'err_msg_serv_already_stop'};
	}
	return "Success";

}

#remote_foreigncall for mdm module
#mdm_restartftp()
sub mdm_restartftp()
{
        my $out;
	$out = `$config{'ftp_service'} restart 2>&1`;
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_starthttp()
sub mdm_starthttp()
{
        my $out;

	$out = &check_service_status("H");
	if($out == 0)
	{
	    $out = `$config{'http_service'} start 2>&1`;
	}
	else
	{
	    return "HTTP ".$text{'err_msg_serv_already_start'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_stophttp()
sub mdm_stophttp()
{
        my $out;

	$out = &check_service_status("H");
	if($out != 0)
	{
	    $out = `$config{'http_service'} stop 2>&1`;
	}
	else
	{
	    return "HTTP ".$text{'err_msg_serv_already_stop'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_restarthttp()
sub mdm_restarthttp()
{
        my $out;
	$out = `$config{'http_service'} restart 2>&1`;
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_startatalk()
sub mdm_startatalk()
{
        my $out;


	$out = &check_afpserver_status();
	if ($out == 0)
	{
	    if(-f $config{'bondinglock'})
	    {
		return $text{'err_msg_atalk_bonding'};
	    }
	    $out = &ez_system($config{'afp_service'},  "start");
	}else
	{
	    return "AppleTalk Share".$text{'err_msg_serv_already_start'};
	}
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_stopatalk()
sub mdm_stopatalk()
{
        my $out;
	$out = &check_afpserver_status();
	if ($out != 0)
	{
	    $out = `$config{'afp_service'} stop 2>&1`;
	}
	else
	{
	    return "AppleTalk Share".$text{'err_msg_serv_already_stop'};
	}

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_restartatalk()
sub mdm_restartatalk()
{
        my $out;

	if(-f $config{'bondinglock'})
	{
	    return $text{'err_msg_atalk_bonding'};
	}

	$out = &ez_system($config{'afp_service'},  "restart");
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_setcifs($servstring, $security, $codepage)
sub mdm_setcifs()
{
        my ($servstring, $security, $codepage, $error_msg, $global);

	$servstring = @_[0];
	$security = @_[1];
	$codepage = @_[2];

	if ($servstring =~ /[\#\;]/)
	{
	        $error_msg = $text{'desc_invalid'};
		return $error_msg;
	}	
	&foreign_require("samba", "samba-lib.pl");
	%econfig = &foreign_config("samba");
	&lock_file($econfig{'smb_conf'});
	$global = &foreign_call("samba", "get_share", "global");

	&foreign_call("samba","setval","server string", $servstring, "")  if ( defined($servstring) );
	&foreign_call("samba","setval","encrypt passwords", "yes", "");
	&foreign_call("samba","setval","null passwords", "no", "");

	&foreign_call("samba","setval","security", $security, "")  if ( defined($security) );
	
	&foreign_call("samba", "delval", "username map");

	&foreign_call("samba","setval","client code page", $codepage) if ( defined($codepage));

	if ($global)
	{
	    &foreign_call("samba","modify_share", "global", "global");
	}
	else
	{
	    &foreign_call("samba","create_share", "global");
	}
	&unlock_file($econfig{'smb_conf'});
	`$config{'cifs_service'} reload`;

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_setftp($maxclientnum, $maxclientperip)
sub mdm_setftp()
{
        my ($maxclientnum, $maxclientperip, $anonypath, %gbconfig);

	$maxclientnum = @_[0];
	$maxclientperip = @_[1];
	$anonypath = "";

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "get_global_config", \%gbconfig);


	$gbconfig{'MaxClientsNumber'} = $maxclientnum if (defined($maxclientnum));
	$gbconfig{'MaxClientsPerIP'} = $maxclientperip if (defined($maxclientperip));

	
	&foreign_call("pureftpd", "set_anonymous_home", $anonypath);
	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "set_global_config", \%gbconfig);

	`$config{'ftp_service'} restart`;

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_setftpanony($maxclientnum, $maxclientperip, $anonymaxclient, $anonydl, $dlunit)
sub mdm_setftpanony()
{
        my ($maxclientnum, $maxclientperip, $anonymaxclient, $anonydl, $dlunit, %gbconfig);
	
	$maxclientnum = @_[0];
	$maxclientperip = @_[1];
	$anonymaxclient = @_[2];
	$anonydl = @_[3];
	$dlunit = @_[4];

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "get_global_config", \%gbconfig);
	
	$gbconfig{'MaxClientsNumber'} = $maxclientnum if (defined($maxclientnum));
	$gbconfig{'MaxClientsPerIP'} = $maxclientperip if (defined($maxclientperip));
	$gbconfig{'PerUserLimits'} =~ s/:.*$/:$anonymaxclient/ if (defined($anonymaxclient));
	$gbconfig{'AnonymousBandwidth'} = $anonydl if (defined ($anonydl) );

	if($dlunit eq "MB")
	{
	    $gbconfig{'AnonymousBandwidth'} *= 1024;	
	}
	$gbconfig{'AnonymousBandwidth'} = sprintf "%.0f" , $gbconfig{'AnonymousBandwidth'};

	&foreign_require("pureftpd", "pureftpd-lib.pl");
	&foreign_call("pureftpd", "set_global_config", \%gbconfig);

	`$config{'ftp_service'} restart`;
	return "Success";
}

#remote_foreigncall for mdm module
#mdm_setatalk($guestallow, $sessiontimeout, $maxsession)
sub mdm_setatalk()
{
        my ($guestallow, $sessiontimeout, $maxsession, %servconf, $oldmaxsession, $servline);


	$guestallow = @_[0];
	$sessiontimeout = @_[1];
	$maxsession = @_[2];

	&foreign_require("netatalk", "netapple-lib.pl");
	&foreign_call("netatalk", "ez_get_afpserver", \%servconf);
	$oldmaxsession = &foreign_call("netatalk", "ez_get_maxsessions");


	if(defined ($maxsession))
	{
	    $maxsession = 20 unless($maxsession);	    
	    $oldmaxsession = $maxsession ;
	}

	if (defined( $guestallow) )
	{
	    if ($guestallow eq "yes")
	    {
		$servconf{'uamlist'} = '-uamlist uams_guest.so,uams_clrtxt.so,uams_dhx.so';
	    }
	    else
	    {
		$servconf{'uamlist'} = '-uamlist uams_clrtxt.so,uams_dhx.so';
	    }
	}

	if (defined( $sessiontimeout))
	{
	    $sessiontimeout = 30 unless($sessiontimeout);
	    $sessiontimeout *= 2;
	    $servconf{'timeout'} = '-timeout ' . $sessiontimeout;
	}

	&foreign_require("netatalk", "netapple-lib.pl");
	$servline = &foreign_call("netatalk", "ez_afp_chg_servline", \%servconf);
	&foreign_call("netatalk", "ez_save_afpserver", $servline);
	&foreign_call("netatalk", "ez_save_maxsessions", $maxsession);

	`$config{'afp_service'} restart`;

	return "Success";
}

#

1;
